package com.guide.dao.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import com.guide.dao.GuideApplyDao;
import com.guide.domain.GuideApply;
import com.guide.domain.GuideApplyQuery;
import com.guide.domain.GuideApplyQuery.Criteria;
import com.guide.persistence.GuideApplyMapper;
import com.guide.utils.ConstantUtils;
@Repository("guideApplyDao")
public class GuideApplyDaoImpl implements GuideApplyDao{

	@Autowired
	private GuideApplyMapper guideApplyMapper;

	@Override
	public GuideApply getGuideApplyByTeamIdAndGuideId(Long guideId, Long teamId) {
		// TODO Auto-generated method stub
		GuideApplyQuery query = new GuideApplyQuery();
		query.createCriteria().andGuideIdEqualTo(guideId).andTeamIdEqualTo(teamId);
		List<GuideApply>  guideApplys = guideApplyMapper.selectByExample(query,null,null);
		if(!CollectionUtils.isEmpty(guideApplys)){
			return guideApplys.get(0);
		}
		return null;
	}

	@Override
	public void insertGuideApply(GuideApply guideApply) {
		// TODO Auto-generated method stub
		guideApplyMapper.insert(guideApply);
	}

	@Override
	public List<GuideApply> getApplyGuidesByTeamId(Long teamId) {
		// TODO Auto-generated method stub
		GuideApplyQuery query = new GuideApplyQuery();
		query.setOrderByClause("ctime desc");
		query.createCriteria().andTeamIdEqualTo(teamId);
		return guideApplyMapper.selectByExample(query,null,null);
	}

	@Override
	public void updateApplyGuide(GuideApply guideApply) {
		// TODO Auto-generated method stub
		GuideApplyQuery query = new GuideApplyQuery();
		Criteria criteria = query.createCriteria();
		criteria.andTeamIdEqualTo(guideApply.getTeamId());
		if(guideApply.getGuideId()!=null) {
			criteria.andGuideIdEqualTo(guideApply.getGuideId());
		}
		guideApplyMapper.updateByExampleSelective(guideApply, query);
	}

	@Override
	public List<GuideApply> getApplyGuidesByGuideId(Long guideId) {
		GuideApplyQuery query = new GuideApplyQuery();
		query.createCriteria().andGuideIdEqualTo(guideId);
		return guideApplyMapper.selectByExample(query,null,null);
	}

	@Override
	public GuideApply getApplyGuideByConfirm(Long teamId) {
		GuideApplyQuery query = new GuideApplyQuery();
		query.createCriteria().andTeamIdEqualTo(teamId).andStatusEqualTo(ConstantUtils.GUIDE_STATUS_1);
		List<GuideApply> guideApplys = guideApplyMapper.selectByExample(query, null, null);
		if(!CollectionUtils.isEmpty(guideApplys)) {
			return guideApplys.get(0);
		}
		return null;
	}
	

}
